<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	
	if (isset($_GET['cs_id']))
	{
		$cs_id = mysql_real_escape_string(strip_tags($_GET['cs_id']));
	}
	if (isset($_GET['building_id']))
	{
		$building_id = mysql_real_escape_string(strip_tags($_GET['building_id']));
	}
	if (isset($_GET['semester_id']))
	{
		$semester_id = mysql_real_escape_string(strip_tags($_GET['semester_id']));
	}
	
	if (isset($_POST['submitAddSchedule']))
	{
		$instructor = mysql_real_escape_string(strip_tags($_POST['instructor']));
		$classroom = mysql_real_escape_string(strip_tags($_POST['classroom']));
		$timeslot = mysql_real_escape_string(strip_tags($_POST['timeslot']));
		
		$query = "INSERT INTO schedule(id, course_section_id, classroom_id, time_slot_id)
				  VALUES('', '$cs_id', '$classroom', '$timeslot')";
		if ($result = mysql_query($query))
		{
			$query = "UPDATE course_sections
					  SET instructor_id = '$instructor' WHERE id = '$cs_id'";
			if ($result = mysql_query($query))
			{
				header("Location:postSchedule.php?building_id=$building_id&semester_id=$semester_id");
				exit(0);
			}
		}
	}
	
	else if (isset($_POST['cancel']))
	{
		header("Location:postSchedule.php?building_id=$building_id&semester_id=$semester_id");
		exit(0);
	}
	
	
	$query = "SELECT *, csub.abbreviation AS sub_abbrev, cs.id AS course_section_id, i.id AS inst_id, c.hours_per_week as course_hours, cs.num_seats AS cs_num_seats, b.id AS building_id
			  FROM course_sections cs
			  JOIN course_section_mapping csm ON csm.course_section_id = cs.id
			  JOIN courses c on c.id = csm.course_id
			  JOIN course_subjects csub ON csub.id = c.course_subject_id
			  JOIN instructors i ON i.id = cs.instructor_id
			  JOIN classroom_types crt ON crt.id = cs.classroom_type_id
			  JOIN building_subject_mapping bsm ON bsm.course_subject_id = csub.id
			  JOIN buildings b ON b.id = bsm.building_id
			  WHERE cs.id = '$cs_id'
			  GROUP BY cs.id";
	
	$result = mysql_query($query); echo mysql_error();
	$row_count = 0;
	$row = mysql_fetch_assoc($result);
	
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');
?>

<script type="text/javascript">

	var current_scheduled_classroom = '0';
	var current_scheduled_timeslot = '0';

	var current_instructor = '<?php echo $row['inst_id']; ?>';
	var current_classroom = '0';
	var current_timeslot = '0';
	var current_building = '<?php echo $row['building_id']; ?>';
	var current_semester = '<?php echo $row['semester_id']; ?>';
	var course_number = '<?php echo $row['course_number']; ?>';
	var cs_num_seats = '<?php echo $row['cs_num_seats']; ?>';
	
	function update_valid_timeslots(current_instructor, current_classroom, current_building, current_semester, course_number, current_scheduled_classroom, current_scheduled_timeslot)
	{	
		$.ajax({
			type: "POST",
			url: "include/query.php",
			data: {update_valid_time_slots:true, current_instructor:current_instructor, current_classroom:current_classroom, current_building:current_building, current_semester:current_semester, course_number:course_number, current_scheduled_classroom:current_scheduled_classroom, current_scheduled_timeslot:current_scheduled_timeslot},
			success: function(html) {
				$('.timeslot_option').removeClass('valid');
				if (html != 'false')
				{
					var valid_timeslots = html.split(',');
					var id = '';
					var current_timeslot_valid = false;
					for (var i = 0; i < valid_timeslots.length; i++)
					{
						id = valid_timeslots[i];
						$('#timeslot_'+id).addClass('valid');
						if (current_timeslot == id)
						{
							current_timeslot_valid = true;
						}
					}
					$('#timeslot_error').empty();
					if (current_timeslot_valid == false && current_timeslot != '0')
					{
						$('#timeslot_error').append('Invalid Timeslot');
					}
					
				}
			}
		});
	}
	
	function update_valid_classrooms(current_instructor, current_timeslot, current_building, current_semester, cs_num_seats, current_scheduled_classroom, current_scheduled_timeslot)
	{
		$.ajax({
			type: "POST",
			url: "include/query.php",
			data: {update_valid_classrooms:true, current_instructor:current_instructor, current_timeslot:current_timeslot, current_building:current_building, current_semester:current_semester, cs_num_seats:cs_num_seats, current_scheduled_classroom:current_scheduled_classroom, current_scheduled_timeslot:current_scheduled_timeslot},
			success: function(html) {
				$('.classroom_option').removeClass('valid');
				if (html != 'false')
				{
					var valid_classrooms = html.split(',');
					var id = '';
					var current_classroom_valid = false;
					for (var i = 0; i < valid_classrooms.length; i++)
					{
						id = valid_classrooms[i];
						$('#classroom_'+id).addClass('valid');
						if (current_classroom == id)
						{
							current_classroom_valid = true;
						}
					}
					$('#classroom_error').empty();
					if (current_classroom_valid == false && current_classroom != '0')
					{
						$('#classroom_error').append('Invalid Classroom');
					}
				}
			}
		});
	}

	$(document).ready(function(){
		update_valid_timeslots(current_instructor, current_classroom, current_building, current_semester, course_number, current_scheduled_classroom, current_scheduled_timeslot);
		update_valid_classrooms(current_instructor, current_timeslot, current_building, current_semester, cs_num_seats, current_scheduled_classroom, current_scheduled_timeslot);
		
		$('#instructor_select').change(function(){
			current_instructor = $('#instructor_select').find(":selected").val();
			update_valid_timeslots(current_instructor, current_classroom, current_building, current_semester, course_number, current_scheduled_classroom, current_scheduled_timeslot);
			update_valid_classrooms(current_instructor, current_timeslot, current_building, current_semester, cs_num_seats, current_scheduled_classroom, current_scheduled_timeslot);
		});
		
		$('#classroom_select').change(function(){
			current_classroom = $('#classroom_select').find(":selected").val();
			update_valid_timeslots(current_instructor, current_classroom, current_building, current_semester, course_number, current_scheduled_classroom, current_scheduled_timeslot);
			update_valid_classrooms(current_instructor, current_timeslot, current_building, current_semester, cs_num_seats, current_scheduled_classroom, current_scheduled_timeslot);
		});
		
		$('#timeslot_select').change(function(){
			current_timeslot = $('#timeslot_select').find(":selected").val();
			update_valid_timeslots(current_instructor, current_classroom, current_building, current_semester, course_number, current_scheduled_classroom, current_scheduled_timeslot);
			update_valid_classrooms(current_instructor, current_timeslot, current_building, current_semester, cs_num_seats, current_scheduled_classroom, current_scheduled_timeslot);
		});
	});
</script>



	<p><a href="postSchedule.php?building_id=<?php echo $building_id; ?>&amp;semester_id=<?php echo $semester_id; ?>">&lt;&lt; Back to Full Schedule</a></p>

	<h1>Schedule Course</h1>
    
    <form name="addSchedule" action="addSchedule.php?cs_id=<?php echo $cs_id; ?>&amp;building_id=<?php echo $building_id; ?>&amp;semester_id=<?php echo $semester_id; ?>" method="post">
	<table class="editSchedule">
	<tr>
	<td class="left_label">Course:</td><td>
	<?php
	$course_section_id = $row['course_section_id'];
	
	$q2 = "SELECT *
		   FROM course_sections cs
		   JOIN course_section_mapping csm ON csm.course_section_id = cs.id
		   JOIN courses c on c.id = csm.course_id
		   JOIN course_subjects csub ON csub.id = c.course_subject_id
		   WHERE cs.id = '$cs_id'
		   ORDER BY c.course_number ASC";
	$r2 = mysql_query($q2);
	$num_courses = mysql_num_rows($r2);
	$count = 0;
	while($row2 = mysql_fetch_assoc($r2))
	{
		$count++;
		echo $row2['abbreviation'].' '.$row2['course_number'].' - ';
		if (strlen($row2['section_number']) == 1)
			{echo "00".$row2['section_number'];}
		else if (strlen($row2['section_number']) == 2)
			{echo "0".$row2['section_number'];}
		else
			{echo $row['section_number'];}
		if ($count == 1)
		{
			echo " (".$row2['title'].")";
		}
		if ($count != $num_courses)
		{
			echo "<br />";
		}
	}
	?>
	</td></tr>
    
    
    <tr>
	<td class="left_label">Instructor:</td><td> 
		<select name="instructor" id="instructor_select">
		<?php 
		$q2 = "SELECT *
			   FROM instructors
			   ORDER BY last_name, first_name";
		$r2 = mysql_query($q2);
		while($row2 = mysql_fetch_assoc($r2))
		{
			echo "<option value=\"".$row2['id']."\" class=\"instructor_option\" id=\"instructor_".$row2['id']."\"";
			if ($row['inst_id'] == $row2['id'])
			{
				echo " selected=\"selected\"";
			}
			echo ">";
			if ($row2['first_name'] != 'STAFF' && $row2['first_name'] != 'TBA')
			{
				echo htmlspecialchars($row2['last_name']).', '.htmlspecialchars($row2['first_name']);
			}
			else
			{
				echo $row2['first_name'];
			}
			echo "</option>\n";
		}
	?></select><span id="instructor_error" class="error_message"></span></td>
    </tr>
    
    <tr>
    <tr>
    <td class="left_label">Preferred Classroom Type:</td>
	<td><?php echo $row['description']; ?></td>
    </tr>
    <tr>
    <tr>
    <td class="left_label">Seats Needed:</td>
	<td><?php echo $row['cs_num_seats']; ?></td>
    </tr>
	<td class="left_label">Room:</td><td> 
		<select name="classroom" id="classroom_select"><?php 
		$q2 = "SELECT *, c.id AS cr_id
				  FROM classrooms c
				  JOIN buildings b ON b.id = c.building_id
				  JOIN classroom_types crt ON crt.id = c.classroom_type_id
				  ORDER BY crt.description, c.num_seats";
		$r2 = mysql_query($q2);
		while($row2 = mysql_fetch_assoc($r2))
		{
			echo "<option value=\"".$row2['cr_id']."\" class=\"classroom_option\" id=\"classroom_".$row2['cr_id']."\"";
			if (isset($classroom) && $classroom == $row2['cr_id'])
			{
				echo " selected=\"selected\"";
			}
			echo ">";
			echo $row2['abbreviation'].' '.$row2['room_num'].' - '.$row2['description'].' ('.$row2['num_seats'].' seats)'; 
			echo "</option>\n";
		}
	?></select><span id="classroom_error" class="error_message"></span></td>
    </tr>
    
    <tr>
	<td class="left_label">Timeslot:</td><td> 
		<select name="timeslot" id="timeslot_select"><?php
		$q2 = "SELECT *
			   FROM time_slots
			   WHERE hours_per_week = '$row[course_hours]'
			   ORDER BY start_time ASC, end_time ASC, Monday DESC, Tuesday DESC, Wednesday DESC, Thursday DESC, Friday DESC, Saturday DESC, Sunday DESC";
		$r2 = mysql_query($q2);
		while($row2 = mysql_fetch_assoc($r2))
		{
			echo"<option value=\"".$row2['id']."\" class=\"timeslot_option\" id=\"timeslot_".$row2['id']."\"";
			if (isset($timeslot) && $timeslot == $row2['id'])
			{
				echo " selected=\"selected\"";
			}
			echo ">";
			$days_of_week = '';
			if ($row2['Monday'])
				$days_of_week .= 'M,';
			if ($row2['Tuesday'])
				$days_of_week .= 'Tu,';
			if ($row2['Wednesday'])
				$days_of_week .= 'W,';
			if ($row2['Thursday'])
				$days_of_week .= 'Th,';
			if ($row2['Friday'])
				$days_of_week .= 'F,';
			if ($row2['Saturday'])
				$days_of_week .= 'Sa,';
			if ($row2['Sunday'])
				$days_of_week .= 'Su,';
			$days_of_week = substr_replace($days_of_week, '', -1, 1);
			echo $days_of_week.' ';
			echo date('g:i', strtotime($row2['start_time'])); 
			if (date('a', strtotime($row2['start_time'])) != date('a', strtotime($row2['end_time'])))
			{
				echo date('a', strtotime($row2['start_time']));
			}
			echo ' - '.date('g:ia', strtotime($row2['end_time']));
			echo "</option>\n";
		}
	
	?> 
		</select><span id="timeslot_error" class="error_message"></span></td>
    </tr>
    <tr>
    	<td colspan="2">&nbsp;</td>
    </tr>
    <tr>
    	<td></td>
        <td><input type="submit" name="submitAddSchedule" value="Schedule Course"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="cancel" value="Cancel"/></td>
    </tr>
    </table>
            
	</form>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>